package com.isoftstone.disk.role.service.impl;

import com.isoftstone.disk.mybatis.entity.PtRoleEntity;
import com.isoftstone.disk.mybatis.flexigrid.PadingRstType;
import com.isoftstone.disk.mybatis.flexigrid.PagingBean;
import com.isoftstone.disk.mybatis.ztree.ZtreeEntity;
import com.isoftstone.disk.role.entity.SearchRoleEntity;
import com.isoftstone.disk.role.repository.RoleDao;
import com.isoftstone.disk.role.service.RoleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Resource
    private RoleDao roleDao;

    @Override
    public PadingRstType<PtRoleEntity> queryRoleListPage(SearchRoleEntity search, PagingBean paging) {
        paging.dealSortName(PtRoleEntity.class);
        PadingRstType<PtRoleEntity> ptRoleEntityPadingRstType = new  PadingRstType<>();
        ptRoleEntityPadingRstType.setPage(paging.getPage());
        List<PtRoleEntity> list = roleDao.queryRoleListByPage(search,paging);
        ptRoleEntityPadingRstType.setRawRecords(list);
        Integer total = roleDao.queryRoleTotal(search);
        ptRoleEntityPadingRstType.setTotal(total);
        ptRoleEntityPadingRstType.putItems();
        return ptRoleEntityPadingRstType;
    }
    @Resource
    private MessageSource messageSource;
    @Override
    public List<ZtreeEntity> queryMenuZtreeListByRoleId(String roleUuid) {
        //查询角色列表
        List<ZtreeEntity> list = roleDao.queryRoleZtreeList();
        //是否被选重
        List<String> roleIdList = roleDao.queryRoleListByUserId(roleUuid);
        for(ZtreeEntity ztreeEntity:list){
            for(String  roleId: roleIdList){
                if(roleId.equals(ztreeEntity.getId())){
                    ztreeEntity.setChecked(true);
                    break;
                }
            }
        }
       return list;
    }

    @Override
    public void saveSelectMenuInfo(String roleId, String menuIds) {
        roleDao.deleteMenuIdByRoleId(roleId);
        String[] menuIdArray = menuIds.split(",");
        for(String menuId:menuIdArray){
            if("".equals(menuId)){
                continue;
            }
            roleDao.insertMenuRefRole(menuId,roleId);
        }
    }
}
